﻿using JsonConversion;
using Newtonsoft.Json;
using RTracker.Models.Common;
using RTracker.Models.ErrorLog;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace RTracker.Areas.Recruitment.Models.ContactPerson
{
    public class MContactPerson
    {
        /// static variable RowSize from config settings to get list of this row size.
        public static int nRowSize = Convert.ToInt32(ConfigurationManager.AppSettings["TotalRecordsInListUser"]);

        /// <summary>
        /// This method is used to create a ContactPerson in the database.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about user's authentication context.</param>
        /// <param name="objCContactPerson">This object contains details of the ContactPerson.</param>
        /// <returns>CStatus which is an object that contain transaction details</returns>
        public CStatus CreateContactPerson(CAuthenticationContext objCAuthenticationContext, ref CContactPerson objCContactPerson)
        {
            CStatus oCStatus = new CStatus();

            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("r_create_contact_person_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@contact_person_id", SqlDbType.Int).Value = objCContactPerson.ContactPersonId;
                    cmd.Parameters.Add("@client_id", SqlDbType.Int).Value = objCContactPerson.ClientId;
                    cmd.Parameters.Add("@contact_person_name", SqlDbType.NVarChar).Value = objCContactPerson.ContactPersonName;
                    cmd.Parameters.Add("@contact_number", SqlDbType.NVarChar).Value = objCContactPerson.ContactNumber;
                    cmd.Parameters.Add("@contact_email", SqlDbType.NVarChar).Value = objCContactPerson.ContactEmail;
                    cmd.Parameters.Add("@designation", SqlDbType.DateTime).Value = objCContactPerson.Designation;
                    cmd.Parameters.Add("@created_by", SqlDbType.Int).Value = objCAuthenticationContext.UserId;

                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    cmd.Parameters["@error"].Direction = ParameterDirection.Output;

                    cmd.ExecuteNonQuery();

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }
            return oCStatus;
        }

        /// <summary>
        /// This method is used to delete a ContactPerson in the database.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about user's authentication context.</param>
        /// <param name="objCContactPerson">This object contains details of the ContactPerson.</param>
        /// <returns>CStatus which is an object that contain transaction details.</returns>
        public CStatus DeleteContactPerson(CAuthenticationContext objCAuthenticationContext, ref CContactPerson objCContactPerson)
        {
            CStatus oCStatus = new CStatus();

            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("r_delete_contact_person_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@created_by", SqlDbType.Int).Value = objCAuthenticationContext.UserId;
                    cmd.Parameters.Add("@contact_person_ids", SqlDbType.NVarChar).Value = objCContactPerson.ContactPersonIds;

                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    cmd.Parameters["@error"].Direction = ParameterDirection.Output;

                    cmd.ExecuteNonQuery();

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }
            return oCStatus;
        }

        /// <summary>
        /// This method is used to update the contact person's detail in the database.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about user's authentication context.</param>
        /// <param name="objCContactPerson">This object contains details of the ContactPerson.</param>
        /// <returns>CStatus which is an object that contain transaction details.</returns>
        public CStatus UpdateContactPerson(CAuthenticationContext objCAuthenticationContext, ref CContactPerson objCContactPerson)
        {
            CStatus oCStatus = new CStatus();

            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("r_update_contact_person_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;

                    cmd.Parameters.Add("@contact_person_id", SqlDbType.Int).Value = objCContactPerson.ContactPersonId;
                    cmd.Parameters.Add("@client_id", SqlDbType.Int).Value = objCContactPerson.ClientId;
                    cmd.Parameters.Add("@contact_person_name", SqlDbType.NVarChar).Value = objCContactPerson.ContactPersonName;
                    cmd.Parameters.Add("@contact_number", SqlDbType.NVarChar).Value = objCContactPerson.ContactNumber;
                    cmd.Parameters.Add("@contact_email", SqlDbType.NVarChar).Value = objCContactPerson.ContactEmail;
                    cmd.Parameters.Add("@designation", SqlDbType.DateTime).Value = objCContactPerson.Designation;
                    cmd.Parameters.Add("@created_by", SqlDbType.Int).Value = objCAuthenticationContext.UserId;

                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    cmd.Parameters["@error"].Direction = ParameterDirection.Output;

                    cmd.ExecuteNonQuery();

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }
            return oCStatus;
        }

        /// <summary>
        /// This method is used to get the ContactPerson's detail in the database.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about user's authentication context.</param>
        /// <param name="objCContactPerson">This object contains details of the ContactPerson.</param>
        /// <returns>CStatus which is an object that contain transaction details.</returns>
        public CStatus GetContactPersonDetail(CAuthenticationContext objCAuthenticationContext, ref CContactPerson objCContactPerson)
        {
            CStatus oCStatus = new CStatus();
            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    SqlCommand cmd = new SqlCommand("r_get_contact_person_detail_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;

                    cmd.Parameters.Add("@contact_person_id", SqlDbType.Int).Value = objCContactPerson.ContactPersonId;
                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;

                    SqlDataAdapter adp = new SqlDataAdapter(cmd);

                    DataTable dt = new DataTable();

                    int rowsAffected = adp.Fill(dt);

                    if (rowsAffected > 0)
                    {
                        objCContactPerson = dt.ConvertToListFromDataTableContactPerson()[0];
                    }

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.DetailSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }
            return oCStatus;
        }

        /// <summary>
        /// Sets the softlock on the record whose Id is passed.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about user's authentication context.</param>
        /// <param name="objCContactPerson">This object contains details of the ContactPerson.</param>
        /// <returns>CStatus which is an object that contain transaction details.</returns>
        public CStatus SetContactPersonSoftLock(CAuthenticationContext objCAuthenticationContext, CContactPerson objCContactPerson)
        {
            CStatus oCStatus = new CStatus();

            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("r_set_contact_person_soft_lock_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@contact_person_id", SqlDbType.Int).Value = objCContactPerson.ContactPersonId;
                    cmd.Parameters.Add("@created_by", SqlDbType.Int).Value = objCAuthenticationContext.UserId;

                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    cmd.Parameters["@error"].Direction = ParameterDirection.Output;

                    cmd.ExecuteNonQuery();

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }
            return oCStatus;
        }

        /// <summary>
        /// Sets the hardlock on the record whose Id is passed.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about user's authentication context.</param>
        /// <param name="objCContactPerson">This object contains details of the ContactPerson.</param>
        /// <returns>CStatus which is an object that contain transaction details.</returns>
        public CStatus SetContactPersonHardLock(CAuthenticationContext objCAuthenticationContext, CContactPerson objCContactPerson)
        {
            CStatus oCStatus = new CStatus();

            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("r_set_contact_person_hard_lock_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@contact_person_id", SqlDbType.Int).Value = objCContactPerson.ContactPersonId;
                    cmd.Parameters.Add("@created_by", SqlDbType.Int).Value = objCAuthenticationContext.UserId;

                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    cmd.Parameters["@error"].Direction = ParameterDirection.Output;

                    cmd.ExecuteNonQuery();

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }
            return oCStatus;
        }

        /// <summary>
        /// Releases the softlock on the record whose Id is passed.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about user's authentication context.</param>
        /// <param name="objCContactPerson">This object contains details of the ContactPerson.</param>
        /// <returns>CStatus which is an object that contain transaction details.</returns>
        public CStatus ReleaseContactPersonSoftLock(CAuthenticationContext objCAuthenticationContext, CContactPerson objCContactPerson)
        {
            CStatus oCStatus = new CStatus();

            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("r_release_contact_person_soft_lock_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@contact_person_id", SqlDbType.NVarChar).Value = objCContactPerson.ContactPersonId;
                    cmd.Parameters.Add("@created_by", SqlDbType.Int).Value = objCAuthenticationContext.UserId;

                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    cmd.Parameters["@error"].Direction = ParameterDirection.Output;

                    cmd.ExecuteNonQuery();

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }
            return oCStatus;
        }

        /// <summary>
        /// Releases the hardlock on the record whose Id is passed.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about user's authentication context.</param>
        /// <param name="objCContactPerson">This object contains details of the ContactPerson.</param>
        /// <returns>CStatus which is an object that contain transaction details.</returns>
        public CStatus ReleaseContactPersonHardLock(CAuthenticationContext objCAuthenticationContext, CContactPerson objCContactPerson)
        {
            CStatus oCStatus = new CStatus();

            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("r_release_contact_person_hard_lock_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@contact_person_id", SqlDbType.NVarChar).Value = objCContactPerson.ContactPersonId;
                    cmd.Parameters.Add("@created_by", SqlDbType.Int).Value = objCAuthenticationContext.UserId;

                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    cmd.Parameters["@error"].Direction = ParameterDirection.Output;

                    cmd.ExecuteNonQuery();

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }
            return oCStatus;
        }

        /// <summary>
        ///  This function is used to get complete User list
        /// </summary>
        /// <returns>List of type CUser</returns>
        public GContactPerson GetContactPersonGridList(CContactPerson objCContactPerson, CAuthenticationContext objCAuthenticationContext)
        {
            GContactPerson objGContactPerson = new GContactPerson();
            int nTotalRecords = 0;
            int nCurrentRowSize = nRowSize;

            if (objCContactPerson.RowSize != 0)
            {
                nCurrentRowSize = objCContactPerson.RowSize;
            }
            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    SqlCommand cmd = new SqlCommand("r_get_contact_person_paging_list_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@contact_person_id", SqlDbType.Int).Value = objCContactPerson.ContactPersonId;
                    cmd.Parameters.Add("@page_number", SqlDbType.Int).Value = objCContactPerson.PageNumber;
                    cmd.Parameters.Add("@row_size", SqlDbType.Int).Value = nCurrentRowSize;
                    cmd.Parameters.Add("@sort_on", SqlDbType.VarChar).Value = objCContactPerson.SortOn;
                    cmd.Parameters.Add("@sort_type", SqlDbType.VarChar).Value = objCContactPerson.SortType;
                    cmd.Parameters.Add("@search", SqlDbType.VarChar).Value = objCContactPerson.Search;

                    cmd.Parameters.Add("@searchon", SqlDbType.VarChar).Value = objCContactPerson.SearchOn;
                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    cmd.Parameters["@error"].Direction = ParameterDirection.Output;
                    SqlDataAdapter adp = new SqlDataAdapter(cmd);

                    DataTable dt = new DataTable();

                    int rowsAffected = adp.Fill(dt);

                    objGContactPerson.Paging = new CPaging();
                    if (rowsAffected > 0)
                    {
                        objGContactPerson.LstContactPerson = dt.ConvertToListFromDataTableContactPerson();
                        objGContactPerson.LstContactPerson[0].PageNumber = int.Parse(cmd.Parameters["@idOut"].Value.ToString());
                        objGContactPerson.Paging.PageNumber = objGContactPerson.LstContactPerson[0].PageNumber;
                    }
                    else
                    {
                        objGContactPerson.LstContactPerson = null;
                    }


                    nTotalRecords = int.Parse(cmd.Parameters["@error"].Value.ToString());

                    objGContactPerson.Status = new CStatus();

                    if (nTotalRecords < 0)
                    {
                        objGContactPerson.Status.Success = false;
                        objGContactPerson.Status.StatusDetails = nTotalRecords.DbError();
                    }
                    else
                    {
                        objGContactPerson.Paging.TotalRecordsCount = nTotalRecords;
                        objGContactPerson.Paging.PageSize = nCurrentRowSize;

                        objGContactPerson.Status.Success = true;
                        objGContactPerson.Status.StatusDetails = "success";
                    }
                }
                catch (Exception ex)
                {
                    objGContactPerson.Status = new CStatus();
                    objGContactPerson.Status.Success = false;
                    objGContactPerson.Status.StatusDetails = ex.Message;
                }
            }
            return objGContactPerson;
        }

    }
}